# General imports
import random
import Vector2 as vec
from Vector2 import Vector

# Game imports
from player import Player
from character import Character

def sequence_generator():
    i = 0
    while True:
        yield i
        i += 1

class Game(object):
    def __init__(self):
        self.tick_count = 0
        self.tickrate = 60.0
        self.timestep = 30.0 / self.tickrate
        self.entity_indices = sequence_generator()
        self.entities = []
        self.players = []
        
        # Create some players
        # TODO: Consider renaming characters to entities
        #       Consider either having entities auto-added to the game instance or
        #        using a game.add_entity( ) method.
        self.players.append(Player("Player 1", self))
        self.players.append(Player("Player 2", self))
        for player in self.players:
            player.entity.pos = Vector(random.random()*200,random.random()*200)
        
    def get_ticks(self):
        return self.tick_count
    def update(self):
        for ent in self.entities:
            if ent.enabled:
                ent.update()
        # Game object interactions
        for e1 in self.entities:
            for e2 in self.entities:
                if e1.enabled and e2.enabled:
                    # Evaluate collisions symmetrically
                    if e2.idx > e1.idx:
                        if not e1.interact_with(e2) and not e2.interact_with(e1):
                            raise Exception("No interaction defined between " \
                                                +str(type(e1))+" and "+str(type(e2)))
    def draw(self, screen):
        for ent in self.entities:
            if ent.enabled:
                ent.draw(screen)
    
    # Game-play rules
    # ---------------
    def reward(self, player):
        # TODO: Are these better here or in the player class?
        player.score += 1
        player.score_since_death += 1
        player.score_since_on_ground += 1
        if player.score_since_death == 3:
            print "Player " + player.name + " is on a killing spree!"
        elif player.score_since_death == 5:
            print "Player " + player.name + " is on a rampage!"
        if player.score_since_on_ground == 2:
            print "Double kill!"
        elif player.score_since_on_ground == 3:
            print "Triple kill!"
        elif player.score_since_on_ground == 4:
            print "Multi-kill!"
    
    def kill(self, char):
        self.respawn(char)
        
    def respawn(self, char):
        if hasattr(char, "player"):
            char.player.respawn()
        # Move the character to a random new location
        char.pos = Vector(random.random()*200, \
                          random.random()*100)
    
    # Called when character c1 squishes character c2
    def on_squished(self, c1, c2):
        print c1.name + " squished " + c2.name + "!"
        c1.bounce()
        # Check that someone cares about the character
        if hasattr(c1, "player"):
            self.reward(c1.player)
        self.kill(c2)
    
    def on_collided(self, c1, c2):
        print c1.name, "and", c2.name, "collided!"
    # ---------------
